package com.iss.supermarket.UserMangerUI;

import com.iss.supermarket.util.entity.SpUser;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class RolePanel extends JFrame{
    //单例模式
    private static RolePanel rolePanel;
    //定义组件
    private JLabel TipsLabel;
    private ButtonGroup buttonGroup;
    private JRadioButton[] RemarkRadioButton;
    private JButton jButton1,jButton2;
    private JList<SpUser> jList;
    private String finalstatement;

    public RolePanel(){
        super("为用户添加权限");

        InitAssembly();
        InitUI();
    }

    private void InitUI(){
        Container container = getContentPane();
        Box verticalBox = Box.createVerticalBox();

        JPanel jPanel1 = new JPanel();
        jPanel1.add(TipsLabel);
        verticalBox.add(jPanel1);

        JPanel jPanel2 = new JPanel();
        for(int i=0;i<5;++i){
            jPanel2.add(RemarkRadioButton[i]);
        }
        verticalBox.add(jPanel2);

        JPanel jPanel3 = new JPanel();
        jPanel3.add(jButton1);
        jPanel3.add(jButton2);
        verticalBox.add(jPanel3);
        container.add(verticalBox);

        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        setSize(400,200);
        setComponentInCenter(this);
        setVisible(true);
    }

    private void InitAssembly(){
        TipsLabel = new JLabel("将选中的用户设置为:");
        TipsLabel.setHorizontalAlignment(SwingConstants.CENTER);
        TipsLabel.setPreferredSize(new Dimension(200,50));
        TipsLabel.setFont(new Font("宋体",Font.BOLD,16));

        buttonGroup = new ButtonGroup();
        RemarkRadioButton = new JRadioButton[5];
        RemarkRadioButton[0] = new JRadioButton("无");
        RemarkRadioButton[1] = new JRadioButton("管理员");
        RemarkRadioButton[2] = new JRadioButton("收银员");
        RemarkRadioButton[3] = new JRadioButton("仓库管理员");
        RemarkRadioButton[4] = new JRadioButton("售后管理员");
        for(int i=0;i<5;++i){
            buttonGroup.add(RemarkRadioButton[i]);
        }

        jButton1 = new JButton("保存");
        jButton1.addActionListener(new ButtonAction());
        jButton2 = new JButton("取消");
        jButton2.addActionListener(new ButtonAction());
    }

    public String getFinalstatement(){
        finalstatement = null;
        for(int i=0;i<RemarkRadioButton.length;++i){
            if(RemarkRadioButton[i].isSelected()){
                finalstatement = RemarkRadioButton[i].getText();
            }
        }
        return finalstatement;
    }

    private class ButtonAction implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            if("保存".equals(e.getActionCommand())){
                //单例模式访问类对象
                UserManagerList userManagerList = UserManagerList.getuserManagerList();
                jList = userManagerList.getjList();
                List<SpUser> spUsers = jList.getSelectedValuesList();
                String remark = null;
                remark = getFinalstatement();
                for(int i=0;i<spUsers.size();++i){
                    if("无".equals(remark))
                        spUsers.get(i).setRemark(null);
                    else
                        spUsers.get(i).setRemark(remark);
                }
                dispose();
            }else if("取消".equals(e.getActionCommand())){
                dispose();
            }
        }
    }

    public static synchronized RolePanel getRolePanel(){
        if(rolePanel == null){
            rolePanel = new RolePanel();
        }
        return rolePanel;
    }

    private void setComponentInCenter(Component component){
        Dimension UISIZAE = component.getSize();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        if (UISIZAE.height > screenSize.height)
            UISIZAE.height = screenSize.height;
        if (UISIZAE.width > screenSize.width)
            UISIZAE.width = screenSize.width;
        component.setLocation((screenSize.width - UISIZAE.width) / 2, (screenSize.height - UISIZAE.height) / 2);
    }
}
